package com.scut.campus.controller;


import com.scut.campus.context.BaseContext;
import com.scut.campus.dto.CommentDto;
import com.scut.campus.dto.PageDTO;
import com.scut.campus.entity.Comment;

import com.scut.campus.infra.mq.RocketMQProducer;
import com.scut.campus.query.CommentQuery;
import com.scut.campus.query.MoreSubCommentPageQuery;
import com.scut.campus.response.Result;
import com.scut.campus.service.CommentService;
import com.scut.campus.service.LikeService;
import com.scut.campus.vo.CommentVo;
import com.scut.campus.vo.SubCommentVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/comment")
@Slf4j
@Tag(name = "评论功能")
public class CommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private LikeService likeService;
    @Autowired
    private RocketMQProducer rocketMQProducer;


    @PostMapping("/insert")
    @Operation(summary = "评论")
    public Result comment(@RequestBody CommentDto commentDto){

        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDto, comment);
        comment.setUserId(BaseContext.getCurrentId());
        //rocketMQProducer.sendMessage("Comment",comment);



        //TODO 是否需要事务？
        commentService.insertComment(commentDto);
        //增加评论数量

        //TODO 通知被评论人

        return Result.success();
    }

    @PostMapping("/queryByPost")
    @Operation(summary = "根据帖子查询需要展示的评论")
    public Result<PageDTO<CommentVo>> queryByPost(@RequestBody CommentQuery commentQuery){


        List<CommentVo> commentVoList=
                commentService.getCommentsByPostId(commentQuery);
        Long total=commentService.commentCount(commentQuery.getPostId());

        PageDTO<CommentVo> pageDTO
                =PageDTO.<CommentVo>builder()
                .total(total)
                .list(commentVoList)
                .build();

        return Result.success(pageDTO);
    }

    @PostMapping("/likeComment/{commentId}")
    @Operation(summary = "点赞评论")
    public Result likeComment(@PathVariable String commentId){
        likeService.likeComment(commentId);
        return Result.success();
    }

    @PostMapping("/queryMoreSubcomment")
    @Operation(summary = "查询更多子评论")
    public Result<List<SubCommentVo>> queryMoreSubcomment(@RequestBody MoreSubCommentPageQuery moreSubCommentPageQuery){
        List<SubCommentVo> subComments=commentService.queryMoreSubcomment(moreSubCommentPageQuery);
        return Result.success(subComments);
    }

    @PostMapping("/dislikeComment/{commentId}")
    @Operation(summary = "取消点赞评论")
    public Result dislikeComment(@PathVariable String commentId){
        likeService.deleteByCommentId(commentId, true);
        return Result.success();
    }

    @PostMapping("/deleteComment/{commentId}")
    @Operation(summary = "删除评论")
    public Result deleteComment(@PathVariable String commentId){
        likeService.deleteByCommentId(commentId,false);
        commentService.removeById(commentId);
        return Result.success();
    }
}
